"use client"

import React, { createContext, useEffect, useState } from 'react';

interface Props {
  children: React.ReactNode;
}

interface AuthContextType {
  currentUser: string | null;
  login: (usename: string) => void;
  logout: () => void;
  isAuthenticated: () => boolean;
  loading: boolean;
}

export const AuthContext = createContext<AuthContextType | null>(null);

export function AuthProvider({ children }: Props) {
  const [currentUser, setCurrentUser] = useState<string | null>(null);
  const [loading, setLoading] = useState<boolean>(false);

  const login = (username: string) => {
    setCurrentUser(username);
    setLoading(true);

    setTimeout(() => {
      setLoading(false);
    }, 500);
  };

  const logout = () => {
    setCurrentUser(null);
  };

  const isAuthenticated = () => {
    return !!currentUser;
  };

  useEffect(() => {}, []);

  const value = {
    currentUser,
    login,
    logout,
    isAuthenticated,
    loading,
  };

  return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}
